package user;

import book.Library;
import constant.Constant;
import utils.PermissionException;

public class ProxyUser {
    private User realUser;

    public ProxyUser(User user) {
        this.realUser = user;
        library = Library.getLibrary();
    }

    public User getRealUser() {
        return realUser;
    }

    private Library library;

    public int display() {
        return this.realUser.display();
    }

    //查找图书、显示图书、退出系统三种操作对于管理员和普通用户来说是共用方法，所以直接定义在Library当中

    //--------------------管理员相关的方法--------------------//

    private void checkRealUserwhetherAdminUser(String msg) {
        if (!(this.realUser instanceof AdminUser)) {
            throw new PermissionException(msg);
        }
    }
    //上架图书
    public void addBook() {
        System.out.println("代理类addBook方法执行了");
        try {
            checkRealUserwhetherAdminUser("普通用户没有权限添加书籍......");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).addBook();
    }
    //图书修改 支持修改书名 作者 类别
    public void updateBook() {
        try {
            checkRealUserwhetherAdminUser("普通用户没有权限更新书籍......");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).updateBook();
    }
    //删除书籍
    public void removeBook() {
        try {
            checkRealUserwhetherAdminUser("普通用户没有权限删除书籍......");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).removeBook();
    }
    //查看每本书的借阅次数
    public void borrowCount() {
        try {
            checkRealUserwhetherAdminUser("普通用户没有权限查看借阅次数......");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).borrowCount();
    }
    //查看最受欢迎的前n本书
    public void generateBook() {
        try {
            checkRealUserwhetherAdminUser("普通用户没有权限查看最受欢迎的前n本书......");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).generateBook();
    }
    //查看库存状态
    public void checkInventoryStatus() {
        try {
            checkRealUserwhetherAdminUser("普通用户没有权限查看库存状态......");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).checkInventoryStatus();
    }
    //查看并移除上架超过一年的图书
    public void checkAndRemoveOldBooks() {
        try {
            checkRealUserwhetherAdminUser("普通方法没有权限查看并移除上架超过一年的图书......");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(realUser)).checkAndRemoveOldBooks();
    }
    //--------------------普通用户相关的方法--------------------//
    private void checkRealUserWhetherNormalUser(String exceptionMessage) {
        if (!(this.realUser instanceof NormalUser)) {
            throw new PermissionException(exceptionMessage);
        }
    }
    //借阅图书
    public void borrowBook() {
        System.out.println("代理类borrowBook方法执行力了......");
        try {
            checkRealUserWhetherNormalUser("管理员请以普通用户的方式借阅图书......");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((NormalUser)(realUser)).borrowBook();
    }
    //归还图书
    public void returnBook() {
        try {
            checkRealUserWhetherNormalUser("管理员请以普通用户的方式归还图书......");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((NormalUser)(realUser)).returnBook();
    }
    //查看个人借阅情况
    public void viewBorrowBooks() {
        try {
            checkRealUserWhetherNormalUser("管理员请以普通用户的方式查看个人借阅情况");
        }catch (PermissionException e) {
            e.printStackTrace();
        }
        ((NormalUser)(realUser)).viewBorrowBooks();
    }

    public void handleOperation(int choice) {
        if (this.realUser instanceof AdminUser) {
            //管理员操作
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIST:
                    library.exist();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                    break;
                default:
                    break;
            }
            }else {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIST:
                    library.exist();
                    break;
                case Constant.BORROW_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_BOOKS:
                    viewBorrowBooks();
                    break;
            }
        }
    }
}
